import {
  getAddressByIdAPI,
  updateAddressAPI
} from '../../../api/address'

Page({
  data: {
    name: '',
    address: '',
    phone: '',
    userId: 0,
    id: 0,
    addressId: 0
  },
  onLoad(options) {
    this.setData({
      id: options.id
    })

    this.getAddressById(this.data.id)

    let user = wx.getStorageSync('user')
    if (user) {
      this.setData({
        userId: user.id
      })
    }
  },
  async onSubmit() {
    let address = {
      name: this.data.name,
      address: this.data.address,
      phone: this.data.phone,
      id: this.data.addressId,
    }
    await updateAddressAPI(address).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '修改成功',
        })
      }
    })
    console.log(address);
  },
  async getAddressById(id) {
    let that = this
    await getAddressByIdAPI(id).then(res => {
      if (res.code == 200) {
        that.setData({
          name: res.result.name,
          address: res.result.address,
          phone: res.result.phone,
          addressId: res.result.id
        })

      }
    })
  }
})